<?php
namespace app\common\service;

use app\common\util\Util;
use think\Model;

class WxMenuService extends BaseService
{
    /**
     * 获取店铺自定义菜单
     */
    public function GetMenu($request){
        $group_id = $request->param('group_id') ? $request->param('group_id'):1;

        $where = [
            'sid' => $request->param('sid'),
            'group_id' => intval($group_id)
        ];

        $menu = db('menu')->where($where)->find();
        if (empty($menu['menu'])) {
            $menu['menu'] = urldecode(json_encode(array('button' => array())));
        }
        if($menu['menu'] == '[]'){$menu['menu']='{}';}
        $menu['group_id'] = $group_id;
        $data['content'] = ['menu'=>$menu['menu'],'group_id'=>$menu['group_id']];
        return $data;
    }
    /**
     * 设置店铺自定义菜单
     */
    public function SetMenu($request,$sid,$group_id){
        $store = db('store')->where(['store_id' =>$sid])->find();
        $host = $store['domain'] ==''?$_SERVER['HTTP_HOST']:$store['domain'];
        foreach( $request['button'] as $key => $val){

            $as_temp1 = array_search("V1003_GOOD", $val);
            if($as_temp1){
                $as[$key] =$as_temp1;

                $_POST['button'][$key]['key'] = "V1003_GOOD";
                $_POST['button'][$key]['type'] = "click";
                unset($_POST['button'][$key]['url']);

            }
            if(!empty($val['sub_button'])){
                foreach( $val['sub_button'] as $key_sub => $val_sub){
                    $as2_temp1 = array_search("V1003_GOOD", $val_sub);

                    if($as2_temp1){
                        $as2[$key][$key_sub] = $as2_temp1;

                        $_POST['button'][$key]['sub_button'][$key_sub]['key'] = "V1003_GOOD";
                        $_POST['button'][$key]['sub_button'][$key_sub]['type'] = "click";
                        unset($_POST['button'][$key]['sub_button'][$key_sub]['url']);

                    }

                }
            }


        }

        foreach ($request['button'] as $k => $v) {
            $request['button'][$k]['name'] = ($v['name']);
        }

        foreach ($request['button'] as $k => $v) {
            if(isset($v['sub_button'])){
                //存在则是二级目录
                foreach ($v['sub_button'] as $sk => $sv) {
                    if($sv['type'] == 'view'){
                        $sub[] = [
                            'type'=>'view',
                            'name'=>urlencode($sv['name']),
                            'url'=>urlencode($sv['url']),
                        ];
                    }else{
                        $sub[] = [
                            'type'=>'click',
                            'name'=>urlencode($sv['name']),
                            'key'=>$sv['key'],
                        ];
                    }
                }
                $data['button'][] = [
                    'name'=>($v['name']),
                    'sub_button'=>$sub,
                ];
                $sub = null;
            }else{
                //不存在则是一级目录
                if($v['type'] == 'view'){
                    $data['button'][] = [
                        'type'=>'view',
                        'name'=>($v['name']),
                        'url'=>urlencode($v['url']),
                    ];
                }else{
                    $data['button'][] = [
                        'type'=>'click',
                        'name'=>($v['name']),
                        'key'=>$v['key'],
                    ];
                }

            }

        }
        $update_data['release'] = urldecode(json_encode($data));
        $update_data['menu'] = urldecode(json_encode($request));
        $update_data['sid'] = $sid;
        $update_data['group_id'] = $group_id ;
        $menu_where = ['sid' => $sid,'group_id'=>$group_id];
        $menu = db('menu')->where($menu_where)->find();
        if ($menu) {
            $flag = db('menu')->where($menu_where)->update($update_data);
        } else {
            $flag = db('menu')->insert($update_data);
        }


        if($flag !== false){
            $weixin = new WeixinService();
            $button = json_decode($update_data['release'],1);

            if($group_id == 2)
            {
                $matchrule = ['group_id'=>100];
                $res =  $weixin->set_menu($sid,$button['button'],$matchrule);
            }
            else
            {
                $res =  $weixin->set_menu($sid,$button['button']);
            }
            if($res->errcode == 0){
                return ['status' => 1];
            }else{
                return ['status' => 0,'msg' =>'抱歉,发布失败！'.$res->errmsg ];
            }
        }else{
            return ['status' => 0,'msg' =>'抱歉,发布失败！数据库保存失败'];
        }
    }

    /**
     * 获取链接列表
     * @param $requset
     */
    public function GetUrl($request){

        $data['content'] = Util::get_url($request->param('sid'));
        return $data;
    }
}